<?php

Yii::import('zii.widgets.grid.CButtonColumn');

class DButtonColumn extends CButtonColumn {

    public $htmlOptions = array();
    public $updateButtonImageUrl = false;
    public $deleteButtonImageUrl = false;
    public $previewButtonImageUrl = false;
    
    public $updateButtonLabel   = '<li class="ui-state-default ui-corner-all"><span class="ui-icon ui-icon-wrench"></span></li>';
    public $deleteButtonLabel   = '<li class="ui-state-default ui-corner-all"><span class="ui-icon ui-icon-trash"></span></li>';
    public $previewButtonLabel  = '<li class="ui-state-default ui-corner-all"><span class="ui-icon ui-icon-search"></span></li>';
    public $separateButtonLabel = '<span class="ui-icon ui-icon-grip-dotted-vertical" style="margin-top:5px;cursor:default;"></span>';
    
    public $updateButtonOptions = array();
    public $deleteButtonOptions = array();
    public $previewButtonOptions = array(
        'target' => '_blank'
    );
    
    public $previewButtonUrl = '';

    public function init() {
        $this->updateButtonOptions['title'] = Yii::t('label', 'update');
        $this->deleteButtonOptions['title'] = Yii::t('label', 'delete');
        $this->previewButtonOptions['title'] = Yii::t('label', 'preview');
        
        if ($this->previewButtonUrl == '') {
            $this->template = '<ul id="icons" class="ui-widget ui-helper-clearfix">'
                . '{update}'
                . $this->separateButtonLabel
                . '{delete}'
                . '</ul>';
        } else {
            $this->template = '<ul id="icons" class="ui-widget ui-helper-clearfix">'
                . '{update}'
                . '{delete}'
                . '{preview}'
                . '</ul>';
            $this->buttons = array(
                'preview' => array (
                    'label'   => $this->previewButtonLabel,
                    'url'     => $this->previewButtonUrl,
                    'imageUrl' => false,
                    'options' => $this->previewButtonOptions,
                ),
            );
        }
        
        parent::init();
    }
    
    protected function initDefaultButtons()
	{
		if($this->viewButtonLabel===null)
			$this->viewButtonLabel=Yii::t('zii','View');
		if($this->updateButtonLabel===null)
			$this->updateButtonLabel=Yii::t('zii','Update');
		if($this->deleteButtonLabel===null)
			$this->deleteButtonLabel=Yii::t('zii','Delete');
		if($this->viewButtonImageUrl===null)
			$this->viewButtonImageUrl=$this->grid->baseScriptUrl.'/view.png';
		if($this->updateButtonImageUrl===null)
			$this->updateButtonImageUrl=$this->grid->baseScriptUrl.'/update.png';
		if($this->deleteButtonImageUrl===null)
			$this->deleteButtonImageUrl=$this->grid->baseScriptUrl.'/delete.png';
		if($this->deleteConfirmation===null)
			$this->deleteConfirmation=Yii::t('zii','Are you sure you want to delete this item?');

		foreach(array('view','update','delete') as $id)
		{
			$button=array(
				'label'=>$this->{$id.'ButtonLabel'},
				'url'=>$this->{$id.'ButtonUrl'},
				'imageUrl'=>$this->{$id.'ButtonImageUrl'},
				'options'=>$this->{$id.'ButtonOptions'},
			);
			if(isset($this->buttons[$id]))
				$this->buttons[$id]=array_merge($button,$this->buttons[$id]);
			else
				$this->buttons[$id]=$button;
		}

		if(!isset($this->buttons['delete']['click']))
		{
			if(Yii::app()->request->enableCsrfValidation)
			{
				$csrfTokenName = Yii::app()->request->csrfTokenName;
				$csrfToken = Yii::app()->request->csrfToken;
				$csrf = "\n\t\tdata:{ '$csrfTokenName':'$csrfToken' },";
			}
			else
				$csrf = '';

			if($this->afterDelete===null)
				$this->afterDelete='function(){}';
            
			$this->buttons['delete']['click']=<<<EOD
function() {
	var params = { msg: "$this->deleteConfirmation" };
    var url = $(this).attr('href');
    
    $("#null").showConfirm(params, function(){
        var th=this;
        var afterDelete=$this->afterDelete;
        $.fn.yiiGridView.update('{$this->grid->id}', {
            type:'POST',
            dataType: 'json',
            url: url, $csrf
            success:function(data) {
                $.fn.yiiGridView.update('{$this->grid->id}');
                afterDelete(th,true,data);
            },
            error:function(XHR) {
                return afterDelete(th,false,XHR);
            }
        });
    }, "");
    return false;
}
EOD;
		}
	}
    
}
